package com.animation_study.mpandroidchart;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.animation_study.R;
import com.animation_study.mpandroidchart.fragment.RadarFragment;
import com.animation_study.mpandroidchart.fragment.BarFragment;
import com.animation_study.mpandroidchart.fragment.BaseFragment;
import com.animation_study.mpandroidchart.fragment.FanFragment;
import com.animation_study.mpandroidchart.fragment.LineFragment;

import java.util.ArrayList;
import java.util.List;

public class MPAndroidChartActivity extends AppCompatActivity {

    private ViewPager vp;
    private RadioGroup rg;
    private List<BaseFragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mpandroid_chart);
        vp = (ViewPager) findViewById(R.id.vp);

        fragments = new ArrayList<>();
        fragments.add(new LineFragment());
        fragments.add(new BarFragment());
        fragments.add(new FanFragment());
        fragments.add(new RadarFragment());
        MyAdapter myAdapter = new MyAdapter(getSupportFragmentManager());
        vp.setAdapter(myAdapter);

        rg = (RadioGroup) findViewById(R.id.rg);
        RadioButton radioButton = (RadioButton) rg.getChildAt(0);
        radioButton.setChecked(true);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                Log.d("asdf", i + "");
                vp.setCurrentItem(i-1);
            }
        });
      /*  vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                rg.check(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });*/
    }

    class MyAdapter extends FragmentPagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
